/*** TABLES AND FIGURES FROM PUBLICLY SUPPLEMENTED SAMPLE ***/

clear all
set more 1
capture log close
set scheme s2mono

// global TRACK_TYPES z r
global TARGET_TYPES zscore rscore
global EXPRESSION_TYPES rho sig
global FRAME_RELATIVE_TYPES vrel mrel

do "paths.do"
cd "$WORKING"

log using "$LOGFILES/txtr_3.log", replace

local depvars t_unadj t_rel
local elemtype elem middle

// Standard Controls
local sdcntrls cgsdzlm
local robcntrls cgnomath cgmisslm
local flexcntrls cgdec10zlm cgdec25zlm cgdec75zlm cgdec90zlm

// Policies
local pol_group cggifted cgdis_phy cgdis_mal cgdis_spe cgdis_oth cgbil_2way ///
		cgbil_non2way cgesl_con cgesl_pull cgtitle1
local pol_qual ctexpa cgclsszavg curric_diff

// Determinants
// Hypothesis 1: District
local det_schooltype cflmagnet cflchart
local det_attitude cntydempa0 dedge_priv0
local det_race cghisp cgblack cgasian cgother cglep cgdisadv

local stdcntrls grade_* year_* cgenrl0 ///
		cflhasg5 cflhasg7 denrtot0 dfvtott0 dgsuburb dgtown dgrural ///
		cgmnzlm

estpost cor `depvars' `elemtype' `stdcntrls' `sdcntrls' `robcntrls' `flexcntrls' `pol_group' `pol_qual' ///
		`det_schooltype' `det_attitude' `det_race', matrix
esttab using "$OUTFILES/txtr_3-cor.csv", replace unstack not noobs compress

/************************************************************
Table -- Summary Statistics
TC1-summ
*************************************************************/

** summary statistics by grade
* counts
global X1 cenrtot cgenrt cgclsszavg
* shares
global X2 trk_zsco_fa trk_zsco_vr trk_zsco_mr cgcmath cgmisslm ///
		cgmale cgwhite cghisp cgblack cgasian cgother cgdisadv cglep lep_con ///
		cgdis_phy cgdis_mal cgdis_spe cgdis_oth dis_any cgspedr ///
		cgbil_2way cgbil_non2way cgesl_con cgesl_pull ///
		cggifted ///
		ctexpa curric_diff ///
		cflhasg4 cflhasg5 cflhasg6 cflhasg7 cflhasg8

foreach vv in all grade magnet charter {
	use "$WORKING/txtr_2", clear
	gen curric_diff = 1 - hindex_predrop
	replace cflmagnet = 99 if cflmagnet == .
	replace cflchart = 99 if cflchart == .
	gen g_all = "all"
	gen g_grade = "grade" + string(grade)
	gen g_magnet = "magnet" + string(cflmagnet)
	gen g_charter = "charter" + string(cflchart)
	replace cgmisslm = cgmisslm / cgenrl
	
	gen lep_con = cgbil_non2way + cgesl_con
	gen dis_any = cgdis_phy + cgdis_oth
	
	estpost tabstat $X1 [weight=cgenrt], by(g_`vv') statistics(mean sd n) columns(statistics) nototal
	esttab using "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_1_`vv'.csv", replace main(mean %9.0fc) aux(sd %9.0fc) nogap nostar unstack noobs nonumber nonote nomtitle
	estpost tabstat $X2 [weight=cgenrt], by(g_`vv') statistics(mean sd N) columns(statistics) nototal
	esttab using "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_2_`vv'.csv", replace main(mean %9.3fc) aux(sd %9.3fc) nogap nostar unstack noobs nonumber nonote nomtitle
	
	gen uniq_cgy = 1
	egen uniq_campus = tag(g_`vv' campus)
	egen uniq_district = tag(g_`vv' distnum)
	collapse (sum) uniq_*, by(g_`vv')
	export delimited "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_3_`vv'.csv", replace
}

local k = 0
foreach vv in all grade magnet charter {
	import delimited "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_1_`vv'.csv", clear varnames(1)
	save "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_1_`vv'.dta", replace
	import delimited "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_2_`vv'.csv", clear varnames(1)
	save "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_2_`vv'.dta", replace
	import delimited "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_3_`vv'.csv", clear varnames(1)
	rename g_* _varname
	xpose, clear varname
	tostring *, replace
	order _varname
	rename _varname v1
	save "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_3_`vv'.dta", replace
	
	use "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_1_`vv'.dta", clear
	append using "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_2_`vv'.dta"
	append using "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_3_`vv'.dta"
	gen rn = _n
	
	save "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_`vv'.dta"
	if (`k' != 0) {
		use "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm.dta", clear
		merge 1:1 rn using "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm_`vv'.dta"
		drop _merge
	}
	save "$OUTFILES/txtr_3_tsumm/txtr_3_tsumm.dta", replace
	
	local k = `k' + 1
}

drop rn
export delimited "$OUTFILES/txtr_3-tsumm.csv", replace


/************************************************************
Table 4: Decompose Tracking Policy Variance
T3-RonR
*************************************************************/

use "$WORKING/txtr_2", clear

keep campus distnum grade year cgenrl cgmnzlm cgsdzlm trk_zsco_fa trk_zsco_mr cegrlevel cgschsz
keep if (grade >= 4) & (grade <= 8)

gen split_all = 1
gen split_elem = (grade <= 5)

egen tmp1 = tag(grade year distnum campus)
egen camp_in_dist = sum(tmp1), by(grade year distnum)
egen min_camp = min(camp_in_dist), by(distnum)
drop tmp*
gen split_mincamp = 99
replace split_mincamp = 1 if (min_camp == 1)
replace split_mincamp = 5 if (min_camp <= 5) & (min_camp > 1)

gen g_state = 1
egen g_d = group(distnum)
egen g_gd = group(grade distnum)
egen g_gyd = group(grade year distnum)
egen g_gydc = group(grade year distnum campus)
egen g_gdc = group(grade distnum campus)

save "$WORKING/temp/txtr_3_t3a.dta", replace

mat t3_out = J(24, 6, .)

local rnnn = ""
foreach ff in fa mr {
    foreach eemm in all elem mid {
	    foreach mmcc in all small medium large {
		    use "$WORKING/temp/txtr_3_t3a.dta", clear
			local rn = 1
			
			local rnn = "fa"
			if ("`ff'" == "mr") {
				local rn = `rn' + 12
				local rnn = "mr"
			}
			
			if ("`eemm'" == "elem") {
			    keep if (grade <= 5)
				local rn = `rn' + 4
				local rnn = "`rnn'" + "_elem"
			}
			else if ("`eemm'" == "mid") {
			    keep if (grade >= 6)
				local rn = `rn' + 8
				local rnn = "`rnn'" + "_mid"
			}
			
			if ("`mmcc'" == "small") {
			    keep if (split_mincamp == 1)
				local rn = `rn' + 1
				local rnn = "`rnn'" + "_small"
			}
			else if ("`mmcc'" == "medium") {
			    keep if (split_mincamp == 5)
				local rn = `rn' + 2
				local rnn = "`rnn'" + "_medium"
			}
			else if ("`mmcc'" == "large") {
			    keep if (split_mincamp == 99)
				local rn = `rn' + 3
				local rnn = "`rnn'" + "_large"
			}
			
			local rnnn = "`rnnn'" + " `rnn'"
			local cn = 0
			foreach gg in g_state g_d g_gd g_gyd g_gydc g_gdc {
			    local cn = `cn' + 1
			    areg trk_zsco_`ff' [w=cgschsz], absorb(`gg')
				mat t3_out[`rn',`cn'] = e(r2)
				
			}
		}
	}
}

di "rnnn = `rnnn'"

mat colnames t3_out = g_state g_d g_gd g_gyd g_gydc g_gdc
mat rownames t3_out = `rnnn'

mat2txt, matrix(t3_out) saving("$OUTFILES/txtr_3-t3.txt") replace


/************************************************************
Figure 3: Incidence of Tracking Across Grades
F3-grade
*************************************************************/

foreach ff in fa mr {
	use "$WORKING/txtr_2", clear
	
	keep if (trk_zsco_`ff' != .)
	local nbins = 20

	gen rho_0 = floor(trk_zsco_`ff' * `nbins') / `nbins'
	gen one0 = 1

	collapse (sum) obs=one0 cgenrl, by(rho_0 grade)
	rename obs cgy
	reshape wide cgy cgenrl, i(rho_0) j(grade)
	reshape long cgy cgenrl, i(rho_0) j(grade)
// 	replace cgy = 0 if cgy == .
// 	replace cgenrl = 0 if cgenrl == .
	** mask
	replace cgy = . if (cgenrl <= 5)
	replace cgenrl = . if (cgenrl <= 5)
	** percentages
	egen tmp1 = sum(cgy), by(grade)
	egen tmp2 = sum(cgenrl), by(grade)
	gen pct_cgy = cgy / tmp1
	gen pct_cgenrl = cgenrl / tmp2
	drop tmp*
	** wide
	reshape wide cgy cgenrl pct_cgy pct_cgenrl, i(rho_0) j(grade)
	** reorder
	sort rho_0
	gen trk_fa_mr = "`ff'"
	order trk_fa_mr rho_0 cgy* cge* pct_cgy* pct_cge*
	save "$WORKING/temp/txtr_3_f3_`ff'.dta", replace
}

use "$WORKING/temp/txtr_3_f3_fa.dta", clear
append using "$WORKING/temp/txtr_3_f3_mr.dta"

export delimited "$OUTFILES/txtr_3-f3.csv", replace


/************************************************************
Appendix Figure B1: Comparison of p-values across approaches
FB1-sig1
*************************************************************/

use "$WORKING/txtr_2", clear

keep trk_zsco_vr trk_zsco_fa trk_zsco_pv ///
		cgclsnum cgschsz campus grade year

gen fs = (trk_zsco_fa / cgclsnum) / ((1 - trk_zsco_fa) / (cgschsz - cgclsnum - 1))
gen ftest_pv = 1 - F(cgclsnum, cgschsz - cgclsnum, fs)


gen rpv = floor(trk_zsco_pv * 20) / 20

gen one0 = 1

collapse (sum) obs=one0 (mean) mean_fpv=ftest_pv ///
		(p05) p05_fpv=ftest_pv (p95) p95_fpv=ftest_pv, by(rpv)

export delimited "$OUTFILES/txtr_3-fb1.csv", replace



/************************************************************
Figure B2: Level of tracking by confidence in tracking, by approach
FB2-sig2
*************************************************************/

use "$WORKING/txtr_2", clear

gen fs = (trk_zsco_fa / cgclsnum) / ((1 - trk_zsco_fa) / (cgschsz - cgclsnum - 1))
gen ftest_pv = 1 - F(cgclsnum, cgschsz - cgclsnum, fs)

gen trp = "trpyes" if (trk_zsco_pv != .) & (trk_zsco_pv <= 0.05)
replace trp = "trpno" if (trk_zsco_pv != .) & (trk_zsco_pv > 0.05)
replace trp = "trpmiss" if (trk_zsco_pv == .)

gen tfp = "tfpyes" if (ftest_pv != .) & (ftest_pv <= 0.05)
replace tfp = "tfpno" if (ftest_pv != .) & (ftest_pv > 0.05)
replace tfp = "tfpmiss" if (ftest_pv == .)

gen tfa = floor(trk_zsco_fa * 20) / 20
gen one0 = 1

foreach vv in trp tfp {
	preserve
	collapse (sum) obs=one0, by(tfa `vv')
	replace obs = . if (obs <= 5)
	reshape wide obs, string i(tfa) j(`vv')
	save "$WORKING/temp/txtr_3_fb2_`vv'.dta", replace
// 	export delimited "$OUTFILES/txtr_3_fb2_`vv'.csv", replace
	restore
}

use "$WORKING/temp/txtr_3_fb2_trp.dta", clear
merge 1:1 tfa using "$WORKING/temp/txtr_3_fb2_tfp.dta"
assert _merge == 3
drop _merge
export delimited "$OUTFILES/txtr_3-fb2.csv", replace


/************************************************************
Figure B3. Distribution of mean simulated R-squared under random and purposeful
assignment, by average class size
FB3-clssz1
*************************************************************/

use "$WORKING/txtr_2", clear

xtile size = cgclsszavg, nq(4)

pctile tmp1 = trk_zsco_rm, nq(100)
for num 1/4: kdensity trk_zsco_rm if (size == X), at(tmp1) generate(rm_X rm_dens_X) nograph
gen rm_bin = rm_1
pctile tmp2 = trk_zsco_pm, nq(100)
for num 1/4: kdensity trk_zsco_pm if (size == X), at(tmp2) generate(pm_X pm_dens_X) nograph
gen pm_bin = pm_1
keep if _n <= 100
keep rm_bin rm_dens_* pm_bin pm_dens_*
order rm_bin rm_dens_* pm_bin pm_dens_*

foreach vv in rm_dens pm_dens {
    foreach gg in 1 2 3 4 {
		replace `vv'_`gg' = . if (`vv'_`gg' == 0)
	}
}

export delimited "$OUTFILES/txtr_3-fb3.csv", replace






/************************************************************
Figure C3: Tracking Across Years
FC3-time
*************************************************************/

foreach ff in fa mr {
	foreach sl in elem mid {
		use "$WORKING/txtr_2", clear

		gen glevel = "unknown"
		replace glevel = "elem" if inlist(grade, 4, 5)
		replace glevel = "mid" if inlist(grade, 6, 7, 8)
		keep if glevel == "`sl'"

		local aa = 20

		gen t0 = floor(trk_zsco_`ff' * `aa') / `aa'
		gen one0 = 1

		gen year_group = (floor((year - 2011) / 3) * 3) + 2011

		collapse (sum) obs=one0 cgschsz, by(t0 year_group)
		rename obs cgy
		reshape wide cgy cgschsz, i(t0) j(year_group)
		reshape long cgy cgschsz, i(t0) j(year_group)
		replace cgy = 0 if cgy == .
		replace cgschsz = . if (cgschsz <= 5)
		reshape wide cgy cgschsz, i(t0) j(year_group)
		order t0 cgy* cgsch*
		drop cgy*
		gen fa_mr = "`ff'"
		gen elem_mid = "`sl'"
		save "$WORKING/temp/txtr_3_fc3_`ff'_`sl'.dta", replace
	}
}

use "$WORKING/temp/txtr_3_fc3_fa_elem.dta", clear
append using "$WORKING/temp/txtr_3_fc3_fa_mid.dta"
append using "$WORKING/temp/txtr_3_fc3_mr_elem.dta"
append using "$WORKING/temp/txtr_3_fc3_mr_mid.dta"

order fa_mr elem_mid t0 cg*
export delimited "$OUTFILES/txtr_3-fc3.csv", replace


/************************************************************
Figure D2: Tracking In Grades 6-8, By Exposure To Younger Grades
FD2-gconfig
*************************************************************/

use "$WORKING/txtr_2", clear

gen crosslevel = cflhasg5 & cflhasg6
gen anylevel = 1
gen onelevel = !crosslevel

gen trk_fa0 = floor(trk_zsco_fa * 20) / 20
gen trk_mr0 = floor(trk_zsco_mr * 20) / 20
gen one0 = 1

save "$WORKING/temp/txtr_3_fc2_0.dta", replace

use "$WORKING/temp/txtr_3_fc2_0.dta", clear

collapse (sum) obs=cgenrl, by(trk_mr0 cegrlevel crosslevel)
gen type0 = "_" + cegrlevel + "_onelevel"
replace type0 = "_" + cegrlevel + "_crosslevel" if crosslevel
keep trk_mr0 type0 obs
replace obs = . if (obs <= 5)
reshape wide obs, string i(trk_mr0) j(type0)

gen fa_mr = "mr"
rename trk_mr0 trk

save "$WORKING/temp/txtr_3_fc2_1.dta", replace

use "$WORKING/temp/txtr_3_fc2_0.dta", clear

collapse (sum) obs=cgenrl, by(trk_fa0 cegrlevel crosslevel)
gen type0 = "_" + cegrlevel + "_onelevel"
replace type0 = "_" + cegrlevel + "_crosslevel" if crosslevel
keep trk_fa0 type0 obs
replace obs = . if (obs <= 5)
reshape wide obs, string i(trk_fa0) j(type0)

gen fa_mr = "fa"
rename trk_fa0 trk

save "$WORKING/temp/txtr_3_fc2_2.dta", replace
append using "$WORKING/temp/txtr_3_fc2_1.dta"

order fa_mr trk obs*

export delimited "$OUTFILES/txtr_3-fc2.csv", replace


log close

